add atomic saving via .tmp files and ReplaceFileW for Windows - #527
Merged
Conversation
Reviewer's GuideAdds atomic file saving support via temporary .tmp files, implementing a cross‑platform io::rename (using ReplaceFileW/MoveFileExW on Windows and ::rename elsewhere) and wiring FileSaver to always write to a .tmp and then atomically rename, while also making fsync() functional on Windows via FlushFileBuffers and exception‑based error reporting. Sequence diagram for atomic file saving with tmp files and io_renamesequenceDiagram
actor User
participant FileSaver
participant QFile
participant io
participant OS
User->>FileSaver: save(targetFilename)
FileSaver->>FileSaver: tmpName = targetFilename + .tmp
FileSaver->>QFile: open(tmpName, WriteOnly)
FileSaver->>QFile: write(data)
FileSaver->>QFile: flush()
FileSaver->>io: fsync(file)
io->>OS: FlushFileBuffers or fsync
OS-->>io: success or error
io-->>FileSaver: return or throw IOException
FileSaver->>io: rename(tmpName, targetFilename)
io->>OS: ReplaceFileW or MoveFileExW or rename
OS-->>io: success or error
io-->>FileSaver: return or throw IOException
FileSaver-->>User: save completed or error
Class diagram for io_namespace helpers and FileSaver tmp_rename integrationclassDiagram
class io {
+bool fsync(QFile file) CAN_THROW
+IOResultEnum fsyncNoexcept(QFile file) noexcept
+void rename(QString from, QString to) CAN_THROW
}
class FileSaver {
-QFile device
-QString filename
+~FileSaver()
-static QString maybe_add_suffix(QString filename)
-static void remove_tmp_suffix(QString filename) CAN_THROW
}
io <.. FileSaver : uses
FileSaver : maybe_add_suffix returns filename + ".tmp"
FileSaver : remove_tmp_suffix calls io.rename(from, to)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In the Windows-specific
fsyncimplementation, consider explicitly checking for an invalid file handle (e.g.,handle == -1) and throwing a clearerIOExceptionbefore callingFlushFileBuffers, so failures are easier to diagnose and don't rely on the Win32 error alone. - In
io::renameon Windows, you may want to includeREPLACEFILE_WRITE_THROUGHin theReplaceFileWflags to better mirror the durability guarantees expected from an atomic save operation in the presence of power loss. - The fallback from
ReplaceFileWtoMoveFileExWwithMOVEFILE_COPY_ALLOWEDcan lose atomicity when the destination does not yet exist, especially across volumes; if atomic replacement semantics are required, it may be safer to detect cross-volume moves and handle them differently or fail explicitly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the Windows-specific `fsync` implementation, consider explicitly checking for an invalid file handle (e.g., `handle == -1`) and throwing a clearer `IOException` before calling `FlushFileBuffers`, so failures are easier to diagnose and don't rely on the Win32 error alone.
- In `io::rename` on Windows, you may want to include `REPLACEFILE_WRITE_THROUGH` in the `ReplaceFileW` flags to better mirror the durability guarantees expected from an atomic save operation in the presence of power loss.
- The fallback from `ReplaceFileW` to `MoveFileExW` with `MOVEFILE_COPY_ALLOWED` can lose atomicity when the destination does not yet exist, especially across volumes; if atomic replacement semantics are required, it may be safer to detect cross-volume moves and handle them differently or fail explicitly.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
nschimme
force-pushed
the
add-atomic-win
branch
from
April 20, 2026 15:50
2d617d0 to
2b78e99
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #527 +/- ##
==========================================
- Coverage 25.40% 25.40% -0.01%
==========================================
Files 519 519
Lines 43109 43113 +4
Branches 4699 4699
==========================================
Hits 10952 10952
- Misses 32157 32161 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
Add atomic file saving support using temporary files and platform-specific rename semantics, including proper fsync handling on Windows.
Bug Fixes:
Enhancements: